package com.tanjun.userservice.web.controller.admin;

import com.tanjun.common.annotation.Edit;
import com.tanjun.common.annotation.Insert;
import com.tanjun.common.model.R;
import com.tanjun.userservice.apiservice.request.dept.DeptIdListRequest;
import com.tanjun.userservice.apiservice.request.dept.DeptListRequest;
import com.tanjun.userservice.apiservice.request.dept.DeptRequest;
import com.tanjun.userservice.apiservice.response.admindept.admin.DeptResponse;
import com.tanjun.userservice.apiservice.response.admindept.admin.DeptTreeResponse;
import com.tanjun.userservice.web.biz.admin.AdminDeptBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.tanjun.common.constants.UrlConstant.URL_AUTH.SYSTEM;
import static com.tanjun.common.constants.UrlConstant.URL_USER.ADMIN;

@RestController
@RequestMapping(ADMIN+"/dept")
@Slf4j
public class BaseAdminDeptController {
    @Autowired
    private AdminDeptBiz adminDeptBiz;

    /**
     * 新增部门
     * @param paramVo 部门参数
     * @return 是否成功
     */
    @PostMapping(SYSTEM+"/add")
    public R addDept(@RequestBody @Validated(Insert.class) DeptRequest paramVo){
        adminDeptBiz.addDept(paramVo);
        return R.OK();
    }

    /**
     * 修改部门
     * @param paramVo 部门参数
     * @return 是否成功
     */
    @PostMapping(SYSTEM+"/update")
    public R updateDept(@RequestBody @Validated(Edit.class) DeptRequest paramVo){
        adminDeptBiz.updateDept(paramVo);
        return R.OK();
    }

    /**
     * 返回部门树形结构
     * @return 树形
     */
    @GetMapping(SYSTEM+"/tree")
    public R<List<DeptTreeResponse>> tree(){
        return R.OK(adminDeptBiz.tree());
    }

    /**
     * 获取部门列表
     * @param paramVo
     * @return 部门列表树
     */
    @PostMapping(SYSTEM+"/list")
    public R<List<DeptTreeResponse>> list(@RequestBody @Validated DeptListRequest paramVo){
        return R.OK(adminDeptBiz.tree());
    }

    /**
     * 根据id批量删除部门
     * @param paramVo id列表
     * @return 是否删除成功
     */
    @PostMapping (SYSTEM+"/delByIds")
    public R delByIds(@RequestBody @Validated DeptIdListRequest paramVo){
        adminDeptBiz.deleteDepList(paramVo);
        return R.OK();
    }
    @GetMapping(SYSTEM+"/getById/{id}")
    public R<DeptResponse> getById(@PathVariable(value = "id") Long id){
        return R.OK(adminDeptBiz.getById(id));
    }
}
